﻿using System;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Routing;
using YuYuKiCms.Internet.Properties;

namespace YuYuKiCms.Internet.Modules
{
    public class LocalizationModule : IHttpModule
    {
        #region IHttpModule

        public void Init(HttpApplication context)
        {
            context.BeginRequest += context_BeginRequest;
        }

        public void Dispose()
        {
            //clean-up code here.
        }

        #endregion

        #region Event

        private void context_BeginRequest(object sender, EventArgs e)
        {
            var httpContextWrapper = new HttpContextWrapper(HttpContext.Current);
            var routeData = RouteTable.Routes.GetRouteData(httpContextWrapper);
            if (null == routeData || 0 == routeData.Values.Count)
                return;

            var lg = routeData.Values["language"] as string;

            if (string.IsNullOrEmpty(lg))
                lg = Settings.Default.LANGUAGE[0];

            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lg);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lg);
        }

        #endregion
    }
}